import { getTicket} from './utils.js';
import 'whatwg-fetch'

class WXJSAPI {
	constructor(scanDeviceResult,initLibSuccess) {
		this.scanDeviceResult = scanDeviceResult
		this.initLibSuccess = initLibSuccess
        this.wx_init_lib_ok = false
        this.device_type = localStorage.getItem('device_type')
        this.wx_init()
	}

	wx_init() {
		getTicket(window.location.href, this.wx_config.bind(this))
	}

	wx_config(data) {
        if (data.errcode !== 0) {
            return;
        }
        const ret = data.data;
        window.wx.config({
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            appId: ret.appId, // 必填，公众号的唯一标识
            timestamp: ret.timestamp, // 必填，生成签名的时间戳
            nonceStr: ret.nonceStr, // 必填，生成签名的随机串
            signature: ret.signature,// 必填，签名，见附录1
            beta: true,
            jsApiList: [
                'configWXDeviceWiFi',
                'openWXDeviceLib',
                'scanQRCode',
                'startScanWXDevice',
                'stopScanWXDevice',
                'onScanWXDeviceResult',
                'onWXDeviceStateChange',
                'onWXDeviceBindStateChange',
                'onWXDeviceStateChange',
            ]
        });
        this.jsapi_ticket = ret.jsapi_ticket
        this.access_token = ret.access_token
        window.wx.ready((function () {
            window.wx.invoke('openWXDeviceLib', { 'connType': 'lan', 'brandUserName': this.device_type }, (function (res) {
                if (res.err_msg === 'openWXDeviceLib:ok') {
                    // alert('openWXDeviceLib:ok')
                    this.wx_init_lib_ok = true
                    this.initLibSuccess()
                }
            }).bind(this));

            window.wx.on('onScanWXDeviceResult', (function (res) {
                // alert(JSON.stringify(res))
                if (res.isCompleted === '0') {
                    let len = res.devices.length
                    var list = []
                    for (var i = 0; i < len; i++) {
                        let device = res.devices[i]
                        list.push({ deviceId: device.deviceId })
                    }
					this.scanDeviceResult(list)
                }
            }).bind(this))
        }).bind(this));

        window.wx.error(function (res) {
            alert('wx error', JSON.stringify(res))
            // config信息验证失败会执行error函数，如签名过期导致验证失败，
            //具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
        });
    }

	//开始扫描设备
	scanDevice() {
		if (this.wx_init_lib_ok) {
            window.wx.invoke('startScanWXDevice', { 'connType': 'lan' }, function (res) {
                // alert(JSON.stringify(res))
			});
		}
    }

    toAirkiss(callback){
        if(this.wx_init_lib_ok) {
            window.wx.invoke('configWXDeviceWiFi',{},function(res) {
                callback(res)
            })
        }
    }

    stopScan(){
        if(this.wx_init_lib_ok) {
            window.wx.invoke('stopScanWXDevice',{},function(res) {})
        }
    }

}
export default WXJSAPI;